package com.jiawa.train.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiawa.train.business.domain.DailyTrain;
import com.jiawa.train.business.domain.DailyTrainExample;
import com.jiawa.train.business.domain.Train;
import com.jiawa.train.business.mapper.DailyTrainMapper;
import com.jiawa.train.business.req.DailyTrainQueryReq;
import com.jiawa.train.business.req.DailyTrainSaveReq;
import com.jiawa.train.common.resp.PageResult;
import com.jiawa.train.common.util.SnowUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class DailyTrainService {
    //reource是使用名称注入的方式而autowired是使用类型注入的方式
    @Resource
    private DailyTrainMapper dailyTrainMapper;

    @Resource
    private TrainService trainService;

    @Resource
    private DailyTrainStationService dailyTrainStationService;

    @Resource
    private DailyTrainCarriageService dailyTrainCarriageService;

    @Resource
    private DailyTrainSeatService dailyTrainSeatService;

    @Resource
    private DailyTrainTicketService dailyTrainTicketService;

    @Resource
    private SkTokenService skTokenService;

    //新增或者更新用户信息，通过判断是否有id决定进行新增操作还是更新操作
    public void save(DailyTrainSaveReq req) {
        //将传递的参数赋给DailyTrain对象,在req中没有id,createTime,updateTime的数据，需要自己进行赋值
        //这里的id使用雪花算法，然后创建时间使用当前时间
        DateTime now = DateTime.now();
        DailyTrain dailyTrain = BeanUtil.copyProperties(req, DailyTrain.class);
        //如果id为空，则进行新增操作
        if(ObjectUtil.isNull(dailyTrain.getId())){
            dailyTrain.setId(SnowUtil.getSnowflakeNextId());
            dailyTrain.setCreateTime(now);
            dailyTrain.setUpdateTime(now);
            dailyTrainMapper.insert(dailyTrain);
        }
        //否则进行更新操作
        else{
            dailyTrain.setUpdateTime(now);
            dailyTrainMapper.updateByPrimaryKeySelective( dailyTrain);
        }
    }

    //如果用户id存在则进行根据id进行查询乘客的信息，如果是用户id不存在则进行查询所有,全部都按分页进行处理
    public PageResult queryDailyTrainList(DailyTrainQueryReq  req) {
        //创建dailyTrainExample对象
        DailyTrainExample dailyTrainExample = new DailyTrainExample();
        dailyTrainExample.setOrderByClause("date desc , code asc");
        //设置查询条件
        DailyTrainExample.Criteria criteria = dailyTrainExample.createCriteria();

        if(ObjectUtil.isNotNull(req.getTrainCode())){
            criteria.andCodeEqualTo(req.getTrainCode());
        }
        if(ObjectUtil.isNotNull(req.getDate())){
            criteria.andDateEqualTo(req.getDate());
        }
        // 先设置分页的参数
        PageHelper.startPage(req.getPage(),req.getPageSize());
        //进行查询数据库
        List<DailyTrain> dailyTrainList = dailyTrainMapper.selectByExample(dailyTrainExample);
        //返回查询结果,转为分页的形式,
        PageInfo<DailyTrain> pageInfo = new PageInfo<>(dailyTrainList);
        //获取数据进行封装
        PageResult pageResult = new PageResult(pageInfo.getTotal(), pageInfo.getList());
        //返回查询结果
        return pageResult;
    }

    public void delete(Long id) {
        dailyTrainMapper.deleteByPrimaryKey(id);
    }


    //根据基础的数据生成每日的车次数据
    @Transactional
    public void genDaily(Date date) {
        //1.根据日期删除某天的车次信息
        DailyTrainExample dailyTrainExample = new DailyTrainExample();
        dailyTrainExample.createCriteria().andDateEqualTo(date);
        dailyTrainMapper.deleteByExample(dailyTrainExample);
        //2.查询所有的车次信息
        List<Train> trainList = trainService.queryAll();
        //3.遍历车次信息，然后生成每日车次信息
        for(Train train : trainList){
            DateTime now = DateTime.now();
            DailyTrain dailyTrain = BeanUtil.copyProperties(train, DailyTrain.class);
            dailyTrain.setId(SnowUtil.getSnowflakeNextId());
            dailyTrain.setDate(date);
            dailyTrain.setCreateTime(now);
            dailyTrain.setUpdateTime(now);
            dailyTrainMapper.insert(dailyTrain);
            //4.生成每日车站信息
            dailyTrainStationService.genDailyStation(date,train.getCode());
            //5.生成每日车厢信息
            dailyTrainCarriageService.genDailyCarriage(date,train.getCode());
            //6.生成每日座位信息
            dailyTrainSeatService.genDailySeat(date,train.getCode());
            //7.生成每日车次的余票的信息
            dailyTrainTicketService.genDailyTicket(dailyTrain,date,train.getCode());
            //8.生成每日车次的令牌信息
            skTokenService.genDailySkToken(date,train.getCode());
        }
        log.info("生成日期【{}】车次、车站、车厢、座位、余票信息完成",date);
    }
}